package com.google.code.sharecontent.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.faces.model.SelectItem;

public class Paging {

	private static int DEFAULT_LINEONPAGE = 20;
	
	private int numberOfPagesDisplayed = 10;
	private Integer lineOnPage =DEFAULT_LINEONPAGE;
	private Integer page = 0;
	private Integer allRows;
	private Integer allPages;
	ArrayList<SelectItem> pagerPages;
	private String currentPages = "";
	
	public Paging() {
	}
	
	public Map<String, String> getLinesOnPage() {
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("10", "10");
		result.put("20", "20");
		result.put("50", "50");
		result.put("100", "100");
		result.put("All", "0");
		return result;
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Paging [page: ").append(this.page);
		sb.append(",allPages ").append(this.allPages);
		sb.append(",allRow ").append(this.allRows);
		sb.append("]");
		return sb.toString();
	}
	
	public void generatePages() {
		Integer numberOfPages = this.allPages;
		Integer resultSetLength = this.allRows;
		pagerPages = new ArrayList<SelectItem>();
		int loverBound = page - getNumberOfPagesDisplayed() / 2;

		int upperBound = loverBound + getNumberOfPagesDisplayed();
		if (numberOfPages <= getNumberOfPagesDisplayed()) {
			loverBound = 0;
			upperBound = numberOfPages;
		}else {
			int delta = 0;
			if (loverBound < 0) {
				delta = 0 - loverBound;
				loverBound = 0;
				upperBound = upperBound + delta;
			} else if (upperBound > numberOfPages) {
				delta = numberOfPages - upperBound;
				upperBound = numberOfPages;
				loverBound = loverBound + delta;
			}
		}
		if (upperBound > numberOfPages) {
			upperBound = numberOfPages;
		}
//		if (upperBound > ((double)this.getResultSetLength() / resultsPerPage)) {
//			upperBound = Math.ceil((double)this.getResultSetLength() / resultsPerPage);
//		}

		if (upperBound - loverBound > 1) {
			for (int i = loverBound; i < upperBound; i++) {
				SelectItem page = new SelectItem();
				Integer lower = lineOnPage * i + 1;
				Integer upper = lineOnPage * (i + 1);
				if (upper > resultSetLength) {
					upper = resultSetLength;
				}
				if (i == this.page) {
					this.setCurrentPages("" + lower + "-" + upper);
				}
				page.setValue(""+i);
				page.setLabel(""+(1+i));
				pagerPages.add(page);
			}
		} else {
			this.setCurrentPages("1-" + getAllRows());
		}
	}
	
	public Integer getLineOnPage() {
		return lineOnPage;
	}
	public void setLineOnPage(Integer lineOnPage) {
		this.lineOnPage = lineOnPage;
	}
	public Integer getPage() {
		return page;
	}
	public String getPageString() {
		return ""+page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}

	public void setAllRows(Integer allRows) {
		this.allRows = allRows;
	}

	public Integer getAllRows() {
		return allRows;
	}

	public void setAllPages(Integer allPages) {
		this.allPages = allPages;
	}

	public Integer getAllPages() {
		return allPages;
	}

	public ArrayList<SelectItem> getPagerPages() {
		return pagerPages;
	}

	public void setPagerPages(ArrayList<SelectItem> pagerPages) {
		this.pagerPages = pagerPages;
	}

	public void setNumberOfPagesDisplayed(int numberOfPagesDisplayed) {
		this.numberOfPagesDisplayed = numberOfPagesDisplayed;
	}

	public int getNumberOfPagesDisplayed() {
		return numberOfPagesDisplayed;
	}

	public void setCurrentPages(String currentPages) {
		this.currentPages = currentPages;
	}

	public String getCurrentPages() {
		return currentPages;
	}

}
